<template>
  <el-container style="height: 100vh">
    <el-header class="header-container">
      <div class="header-right">
        <div class="welcome-message">欢迎你啊, {{ username }}</div>
        <el-button type="primary" @click="logout" class="logout-button">登出</el-button>
      </div>
    </el-header>
    <el-container>
      <el-aside width="200px" class="menu-container">
        <el-menu
            :default-openeds="['1', '2', '3']"
            class="el-menu-vertical-demo"
            @open="handleOpen"
            @close="handleClose"
            background-color="#2c3e50"
            text-color="#fff"
            active-text-color="#ffd04b"
        >
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-star"></i>
              <span>记账管理</span>
            </template>
            <el-menu-item index="1-1" @click="navigateTo('daily-expense-management')">日常开销管理</el-menu-item>
            <el-menu-item index="1-2" @click="navigateTo('major-expense-management')">重大花销事项管理</el-menu-item>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-star"></i>
              <span>生活管理</span>
            </template>
            <el-menu-item index="2-1" @click="navigateTo('study-management')">学习管理</el-menu-item>
            <el-menu-item index="2-3" @click="navigateTo('travel-management')">旅游管理</el-menu-item>
          </el-submenu>
          <el-submenu index="3">
            <template slot="title">
              <i class="el-icon-star"></i>
              <span>饮食健康管理</span>
            </template>
            <el-menu-item index="3-1" @click="navigateTo('menu-management')">菜单管理</el-menu-item>
            <el-menu-item index="3-2" @click="navigateTo('weight-management')">运动管理</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-container>
        <el-main class="main-container">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import axios from 'axios';

export default {
  name: 'DashboardPage',
  data() {
    return {
      username: localStorage.getItem('username') || '用户'
    };
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log('Open: ', key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log('Close: ', key, keyPath);
    },
    navigateTo(routeName) {
      if (this.$route.name !== routeName) {
        this.$router.push({ name: routeName });
      }
    },
    logout() {
      axios.get('/logout')
          .then(response => {
            if (response.data.code === 200) {
              console.log('登出成功');
              localStorage.removeItem('username'); // 清除用户名
              this.$router.push({ name: 'LoginPage' });
            } else {
              console.error('登出失败:', response.data.message);
            }
          })
          .catch(error => {
            console.error('登出错误:', error);
          });
    }
  }
};
</script>

<style scoped>
.menu-container {
  background-color: #2c3e50; /* 主菜单背景颜色 */
}
.el-menu-vertical-demo {
  border-right: none;
}

/* 主菜单样式 */
.el-menu-vertical-demo > .el-submenu > .el-submenu__title {
  background-color: #2c3e50; /* 主菜单背景颜色 */
  color: #fff; /* 主菜单文字颜色 */
}

/* 主菜单悬停样式 */
.el-menu-vertical-demo > .el-submenu > .el-submenu__title:hover {
  background-color: #1f2d3a; /* 主菜单悬停背景颜色 */
}

/* 子菜单样式 */
.el-menu-vertical-demo > .el-submenu > .el-menu {
  background-color: #3a3f47; /* 子菜单背景颜色 */
}

/* 子菜单文字颜色 */
.el-menu-vertical-demo > .el-submenu > .el-menu .el-menu-item {
  color: #bfcbd9; /* 子菜单文字颜色 */
}

/* 子菜单悬停样式 */
.el-menu-vertical-demo > .el-submenu > .el-menu .el-menu-item:hover {
  background-color: #2d3136; /* 子菜单悬停背景颜色 */
}

.header-container {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 0 20px;
  background-color: #2c3e50; /* 与菜单相同的背景颜色 */
}

.header-right {
  display: flex;
  align-items: center;
}

.welcome-message {
  font-size: 16px;
  color: #fff;
  margin-right: 20px; /* 与登出按钮的间距 */
}

.logout-button {
  color: #fff;
  background-color: #f56c6c;
  border: none;
}
</style>
